<!-- 车辆管理 -->
<template>
  <div class="dashboard-container driver customer-list-box">
    <!-- 顶部表单部分 -->
    <el-card class="input-card">
      <el-form
        ref="queryform"
        :model="queryData"
        :rules="rules"
      >
        <el-row :gutter="20">
          <el-col :span="8"><el-form-item
            label="司机账号:"
            prop="account"
          >
            <el-input v-model="queryData.account"></el-input>
          </el-form-item></el-col>
          <el-col :span="8"><el-form-item
            label="司机姓名:"
            prop="name"
          >
            <el-input v-model="queryData.name"></el-input>
          </el-form-item></el-col>
          <el-col :span="8"><el-form-item
            label="司机手机号:"
            prop="phone"
          >
            <el-input v-model="queryData.phone"></el-input>
          </el-form-item></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item
              label="所属机构:"
              prop="agentId"
            >
              <div style="display: flex;">
                <Treeselect
                  v-model="queryData.agentId"
                  :options="treeData"
                  :normalizer="normalizer"
                  :serchable="true"
                  style="width: 280px"
                  placeholder="请选择机构"
                  @input="treeChange"
                  @open="open"
                  @close="close"
                />
                <img
                  ref="arrow"
                  src="../../assets/pullDown.png"
                  style="position:absolute;width:18px;height:18px;right:60px;top:12px;cursor:pointer;pointer-events:none;transition-duration:0.3s"
                />
              </div>

            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button
                type="goon"
                @click="btnSearch"
              >搜索</el-button>
              <el-button
                type="goon2"
                @click="btnReset"
              >重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 表格部分 -->
    <el-card class="table-card">
      <el-table
        v-loading="isLoading"
        :data="driverList"
        style="width: 100%;"
        :stripe="true"
      >
        <el-table-column
          label="序号"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ (scope.$index+1)+(queryData.page-1)*queryData.pageSize }}
          </template>

        </el-table-column>
        <el-table-column
          prop="account"
          label="司机账号"
          min-width="200"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="司机姓名"
          min-width="200"
        >
        </el-table-column>
        <el-table-column
          prop="mobile"
          label="司机电话"
          min-width="200"
        >
        </el-table-column>
        <el-table-column
          label="关联车辆"
          min-width="200"
          prop="truck.licensePlate"
        >
        </el-table-column>
        <el-table-column
          prop="agency.name"
          label="所属机构"
          min-width="200"
        >
          <template v-slot="{row}">
            <span>{{ row.agency.name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="司机工作状态"
          prop="workStatus"
          min-width="200"
        >
          <template v-slot="{row}">
            <!-- 0 休息红色 1上班绿色   -->
            <span v-if="row.workStatus === 0"> <div class="red"></div>休息</span>
            <span v-else-if="row.workStatus === 1"> <div class="green"></div>上班</span>
            <span
              v-else
              style="color: #ccc;"
            >--待添加--</span>
            <div class="bigfool"></div>
          </template>
        </el-table-column>

        <el-table-column
          fixed="right"
          label="操作"
          width="150"
        >
          <template v-slot="{ row }">
            <el-button
              type="text"
              size="small"
              @click="checkDriverInformation(row.userId)"
            >查看 </el-button>
            <span style="color: #ccc;">|</span>
            <el-button
              type="text"
              size="small"
              @click="configureTheVehicle(row)"
            > 配置车辆</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 确认dialog -->
      <div class="dilaog-diy">
        <el-dialog
          title="车辆配置"
          :visible.sync="dialogVisible"
          width="30%"
        >
          <p>
            配置车辆需满足以下条件：
          </p>
          <div class="dialog-message">
            <span>①司机信息已完善</span>
            <span>②司机已设置排班</span>
            <span>③司机绑定车辆在未启用状态</span>
          </div>
          <span
            slot="footer"
            class="dialog-footer"
          >
            <el-button
              type="goon2"
              @click="dialogVisible = false"
            >取 消</el-button>
            <el-button
              type="goon"
              @click="dialogHandlerOk"
            >确 定</el-button>
          </span>
        </el-dialog>
      </div>

      <CarConfig
        v-if="isShowCarDialog"
        :is-show-car-dialog.sync="isShowCarDialog"
        :driver-info="driverInfo"
      ></CarConfig>
      <div class="pagination-container">
        <el-pagination
          style="height: 30px"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="queryData.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>
<script>
import { driverList } from '@/api/transit'
import { agencyList } from '@/api/institutions'
import CarConfig from './components/car-config.vue'
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
// import SelectTree from './components/select-tree'
export default {
  name: 'Courses',
  components: {
    CarConfig,
    Treeselect
  },
  data() {
    return {
      counts: 0,
      driverList: [],
      isLoading: false,
      isShowCarDialog: false,
      driverId: null,
      queryData: {
        account: '', // 账号
        name: '', // 姓名
        phone: '', // 手机号
        agentId: null, // 机构id
        page: 1,
        pageSize: 10
      },
      rules: {
        account: [
          { pattern: /^[A-Za-z0-9]{0,20}$/,
            message: '请输入英文/数字字符，最大20个字符',
            trigger: 'blur'
          }
        ],
        name: [
          { max: 10,
            message: '姓名不能超过10个字符',
            trigger: 'blur'
          }],
        phone: [{ max: 11,
          pattern: /^\d+$/,
          message: '请输入手机号',
          trigger: 'blur' }],
        agentId: []
      },
      dialogVisible: false,
      carId: '',
      driverInfo: {},
      treeData: []
    }
  },
  created() {
    this.getDriverList(this.queryData)
    // this.GetInstitutionalMenu()
    this.agencyList()
  },
  methods: {
    /**
     * 获取司机列表数据
    */
    async getDriverList(data) {
      const res = await driverList(data)
      this.counts = +res.data.counts
      this.driverList = res.data.items
    },
    /**
     * 搜索按钮
    */
    btnSearch() {
      this.$refs.queryform.validate((isOk) => {
        if (isOk) {
          this.getDriverList(this.queryData)
        } else {
          this.$message.error('请输入正确的搜索选项')
        }
      })
    },
    /**
     * 重置按钮
    */
    btnReset() {
      this.queryData = {
        account: '', // 账号
        name: '', // 姓名
        phone: '', // 手机号
        agentId: null, // 机构id
        page: 1,
        pageSize: 10
      }
      this.getDriverList(this.queryData)
    },
    /**
     * 分页器
    */
    handleSizeChange(val) {
      this.isLoading = true
      setTimeout(() => {
        this.queryData.pageSize = val
        this.getDriverList(this.queryData)
        this.isLoading = false
      }, 1800)
    },
    handleCurrentChange(val) {
      this.isLoading = true
      // 这里加钱可以优化
      setTimeout(() => {
        this.queryData.page = val
        this.getDriverList(this.queryData)
        this.isLoading = false
      }, 1800)
    },
    checkDriverInformation(id) {
      // this.$router.push({ path: 'driver-detail', query: { id }})
      this.$router.push({ path: `driver-detail/?id=${id}&formUrlName=司机管理` })
    },
    /**
     * 配置车辆
    */
    configureTheVehicle(row) {
      // console.log(id)
      this.dialogVisible = true
      this.driverInfo = row
    },
    // 弹层确认按钮
    dialogHandlerOk() {
      this.dialogVisible = false
      this.isShowCarDialog = true
    },
    // 获取树形菜单数据
    async agencyList() {
      const { data } = await agencyList()
      this.treeData = JSON.parse(data)
      // console.log(this.treeData)
    },
    // 配置树节点
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    // 监听树形菜单下拉获取id
    treeChange(value) {
      // 在此处获取id
      this.queryData.agentId = value
      // console.log(value)
    },
    // 调整图片位置
    open() {
      this.$refs.arrow.style.transform = 'rotate(-180deg)'
    },
    close() {
      this.$refs.arrow.style.transform = 'rotate(0deg)'
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.input-card {
  margin: 18px 0px;
   .el-input {
     width: 280px;
     margin-right:px;
  }

}
// 配置按钮颜色

.el-button--goon:focus,
.el-button--goon:hover {
  background: #ffab98;
  border-color: #ffab98;
  color: #fff;
}

.el-button--goon {
  color: #FFF;
  background-color: #e15536;
  border-color: #e15536;
}
// ---第二个按钮
.el-button--goon2:focus,
.el-button--goon2:hover {
  background: #ffeeeb;
  border-color: #e15536;
  color: #e15536;
}

.el-button--goon2 {
  background-color: #fff;
  border-color: #ccc;
}
.dashboard-container {
  padding: 18px 18px 36px 18px;
}
// 原项目自带
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
// 下拉框
::v-deep .el-select {
  width: 100%;
}

::v-deep .el-card{
  overflow: initial;
}
.dialog-message {
  display: flex;
  flex-wrap: wrap;
  span {
    display: block;
    width: 200px;
    margin-bottom: 14px;
  }
}
.select_tree-drop{
    .org-select-item { height: auto; background: #fff; overflow: auto;}
}
.red {
  width: 8px;
  height: 8px;
  display: inline-block;
  background-color: red;
  border-radius: 50%;
  margin-right: 5px;
}
.green {
  width: 8px;
  height: 8px;
  display: inline-block;
  background-color: green;
  border-radius: 50%;
  margin-right: 5px;
}
.pagination-container {
  display: flex;
  height: 60px;
  justify-content: center;
  align-items: center;

}
.table-card {
  margin-bottom: 30px;
}

</style>
<style>
.el-pagination.is-background .el-pager li:hover{
   color: #e15536 !important;/*hover时候的文字颜色*/
 }
 .el-pager li.active{
   color:#e15536  !important;/*仅设置active时的文字颜色*/
 }
 .el-pagination .el-pager li:not(.disabled):hover {
  color: #e15536;
 }
.el-pagination .btn-prev:hover,
.el-pagination .btn-next:hover{
  color: red !important; /* 这里可以修改为你想要的箭头颜色 */
}
</style>

